package com.thermoberg.passport.controllor;

import com.thermoberg.passport.entity.SbyPassport;
import com.thermoberg.passport.service.SbyPassportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by wanghe on 2016/11/3.
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminAuthorityController {

    @Autowired
    private SbyPassportService sbyPassportService;

    private final String FAIL_USER = "{\"code\": 2, \"msg\": \"用户名已存在！\"}";

    @RequestMapping(value = "/adduser", method = RequestMethod.GET)
    public String adduser() {
        return "insert_user";
    }

    @RequestMapping(value = "/insertuser", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody String insertuser(@ModelAttribute SbyPassport sbyPassport) {
        List<SbyPassport> sbyPassportList = sbyPassportService.sbyPassportList();
        String sby = "";
        for(int i=0;i<sbyPassportList.size();i++){
            sby = sby+sbyPassportList.get(i).getUsername()+",";
        }
        String[] splitString = sby.split(",");
        for(int i=0;i<splitString.length;i++){
            if(splitString[i].equals(sbyPassport.getUsername())){
                return FAIL_USER;
            }
        }
        sbyPassportService.insertuser(sbyPassport);
        return "{\"code\":%d}".replace("%d", "1");
    }

    @RequestMapping(value = "/updateuser", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody String updateuser(@ModelAttribute SbyPassport sbyPassport) {
        sbyPassportService.updateuser(sbyPassport, sbyPassport.getId());
        return "{\"code\":%d}".replace("%d", "1");
    }

    @RequestMapping(value = "/deleteuser", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody String deleteuser(@RequestParam Integer id) {
        sbyPassportService.deleteuser(id);
        return "{\"code\":%d}".replace("%d", "1");
    }

    @RequestMapping(value = "/userview", method = RequestMethod.GET)
    public String userview(ModelMap model) {
        model.addAttribute("sbyPassportList",sbyPassportService.sbyPassportList());
        return "userview_information";
    }

    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public @ResponseBody String resetPwd(@RequestParam Integer id) {
        sbyPassportService.resetPwd(id);
        return "{\"code\":%d}".replace("%d", "1");
    }

    @RequestMapping(value = "/jumpUpdatePage", method = RequestMethod.GET)
    public String jumpUpdatePage(@RequestParam Integer id,ModelMap model) {
        model.addAttribute("sbyPassportList",sbyPassportService.sbyPassportList(id));
        model.addAttribute("id",id);
        return "update-userinformation";
    }


}
